﻿using PRO.DbProvider;
using PRO.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PRO.ORM;

namespace PRO.Product
{
    public class ClassLib
    {
        /// <summary>
        /// 获取所有产品分类
        /// </summary>
        /// <returns></returns>
        public static DataTable GetClass()
        {
            return DataBaseProvider.GetInstance().GetClass();
        }
        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddClass(ClassInfo info)
        {
            return DataBaseProvider.GetInstance().AddClass(info);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool DelClass(int id, out string msg)
        {
            return DataBaseProvider.GetInstance().DelClass(id, out msg);
        }
        /// <summary>
        /// 获取分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static ClassInfo GetClassInfo(int id)
        {
            ClassInfo info = new ClassInfo();
            info.BindDataToEntity(DataBaseProvider.GetInstance().GetClassInfo(id));
            return info;
        }
        /// <summary>
        /// 编辑分类
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool EditClass(ClassInfo info)
        {
            return DataBaseProvider.GetInstance().EditClass(info);
        }
        #region 递归生成自定义树形菜单
        public static DataTable GetOurClassTree()
        {
            DataTable al = new DataTable();
            al.Columns.Add("id", typeof(System.Int32));
            al.Columns.Add("pid", typeof(System.Int32));
            al.Columns.Add("ClassName", typeof(System.String));
            al.Columns.Add("sort", typeof(System.Int32));
            DataTable dt = ClassLib.GetClass();
            ShowTree(dt, 0, al, 0);
            return al;
        }
        public static void ShowTree(DataTable dt, int n, DataTable al, int level)
        {
            //递归打开树型结构
            DataRow[] dr = dt.Select("PID=" + n);
            string space = null;
            for (int j = 0; j < level; j++)
            {
                space += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            if (dr.Length > 0)
            {
                level += 1;
            }

            for (int i = 0; i < dr.Length; i++)
            {
                DataRow row = dr[i];
                DataRow tr = al.NewRow();
                tr[0] = row["id"].ToString();
                //判断是否是最后一个
                if (i == dr.Length - 1)
                {
                    tr[1] = row["pid"].ToString();
                    tr[2] = space + "└ " + row["ClassName"].ToString();
                    tr[3] = row["sort"].ToString();
                }
                else
                {
                    tr[1] = row["pid"].ToString();
                    tr[2] = space + "├" + row["ClassName"].ToString();
                    tr[3] = row["sort"].ToString();
                }
                al.Rows.Add(tr);
                ShowTree(dt, (int)row["ID"], al, level);
            }
        }
        #endregion
    }
}